package main
import (
	"fmt"
	// "time"
)

/*
select语句可以使一个go程可以等待多个通信操作
select会阻塞到某个分支可以继续执行为止，这时就会执行该分支。当多个分支都准备好时会随机选择一个执行。
*/
func fibonacci(c, quit chan int){
	x, y := 0, 1
	for {
		select {
		case c <- x:
			// time.Sleep(1000*time.Millisecond)
			x, y = y, x+y
		case <- quit:
			fmt.Println("quit")
			return
		//当select中的其他分支都没有准备好，default分支就会执行
		default:
			fmt.Println("start")
		}
	}
}
func main() {
	c := make(chan int)
	quit := make(chan int)
	//func(){}()  表示定义了一个可以立即执行的函数
	go func(){
		for i :=0; i < 10; i++{
			fmt.Println(<-c)
		}
		quit <- 0
	}()
	fibonacci(c, quit)
	/*
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	0
	start
	start
	start
	start
	start
	start
	1
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	1
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	2
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	3
	start
	start
	start
	start
	5
	start
	start
	start
	start
	start
	start
	start
	start
	8
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	13
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	start
	21
	start
	start
	start
	start
	start
	start
	start
	start
	34
	start
	quit
	*/

}